import { ref, reactive } from 'vue';
import type { RealtimeData, MarkPoint } from '@/types/chart';
import { processData } from '@/utils/chartGenerator';
import { echartsParamsManager } from '@/storage/EchartsPrograms';

export function useDataProcessor() {
  // 标记点数据
  const markPointXList = ref<MarkPoint[]>([]);
  const markPointYList = ref<MarkPoint[]>([]);
  const markPointPolXList = ref<MarkPoint[]>([]);
  const markPointPolYList = ref<MarkPoint[]>([]);

  // 处理接收到的实时数据
  const processRealtimeData = (data: RealtimeData): RealtimeData => {
    // 数据范围过滤
    const filteredData = {
      ...data,
      X1: processData(-echartsParamsManager.xdefault.value, echartsParamsManager.xdefault.value, data.X1),
      X2: processData(-echartsParamsManager.xdefault.value, echartsParamsManager.xdefault.value, data.X2),
      Y1: processData(5000, 7000, data.Y1),
      Y2: processData(5000, 7000, data.Y2)
    };

    // 处理支柱点
    if (data.SupportList && data.SupportList.length > 0) {
      console.log('接收到支柱点');
      data.SupportList.forEach((supportPoint: any) => {
        const dataIndex = data.X.indexOf(supportPoint);
        if (dataIndex !== -1) {
          const xValue = data.X1[dataIndex];
          const yValue = data.Y1[dataIndex];
          const pointStr = supportPoint.toString();
          markPointPolXList.value.push({ coord: [pointStr, xValue] });
          markPointPolYList.value.push({ coord: [pointStr, yValue] });
        }
      });
    }

    // 处理吊悬点
    if (data.SuspensionList && data.SuspensionList.length > 0) {
      console.log('接收到吊悬点');
      data.SuspensionList.forEach((suspensionPoint: any) => {
        const dataIndex = data.X.indexOf(suspensionPoint);
        if (dataIndex !== -1) {
          const xValue = data.X1[dataIndex];
          const yValue = data.Y1[dataIndex];
          const pointStr = suspensionPoint.toString();
          markPointXList.value.push({ coord: [pointStr, xValue] });
          markPointYList.value.push({ coord: [pointStr, yValue] });
        }
      });
    }

    return filteredData;
  };

  // 清理过期的标记点
  const cleanupMarkPoints = (currentXData: string[]) => {
    if (currentXData.length === 0) return;

    const firstXValue = currentXData[0];

    // 清理吊悬点
    if (markPointXList.value.length > 0) {
      markPointXList.value = markPointXList.value.filter(point => {
        return point.coord[0] >= firstXValue;
      });
      markPointYList.value = markPointYList.value.filter(point => {
        return point.coord[0] >= firstXValue;
      });
    }

    // 清理支柱点
    if (markPointPolXList.value.length > 0) {
      markPointPolXList.value = markPointPolXList.value.filter(point => {
        return point.coord[0] >= firstXValue;
      });
      markPointPolYList.value = markPointPolYList.value.filter(point => {
        return point.coord[0] >= firstXValue;
      });
    }
  };

  // 将数据映射到图表系列
  const mapDataToSeries = (
    chartData: RealtimeData,
    selectedChannels: Array<{ name: string }>
  ): Array<number[] | (number | null)[]> => {
    const seriesData: Array<number[] | (number | null)[]> = [];

    selectedChannels.forEach(channel => {
      switch (channel.name) {
        case '拉出值':
          // 可以选择显示X1或X2，这里默认显示X1
          seriesData.push(chartData.X1);
          seriesData.push(chartData.X2);
          break;
        case '导高':
          // 可以选择显示Y1或Y2，这里默认显示Y1
          seriesData.push(chartData.Y1);
          seriesData.push(chartData.Y2);
          break;
        case '速度':
          seriesData.push(chartData.SpeedList || []);
          break;
        case '磨耗':
          seriesData.push(chartData.MoHaoList1 || []);
          break;
        case '加速度':
          // 如果有加速度数据，在这里映射
          seriesData.push([]);
          break;
        case '网压':
          // 如果有网压数据，在这里映射
          seriesData.push([]);
          break;
        default:
          seriesData.push([]);
      }
    });

    return seriesData;
  };

  // 获取通道对应的标记点
  const getMarkPointsForChannel = (channelName: string): MarkPoint[] => {
    if (channelName.includes('拉出值')) {
      return [...markPointXList.value, ...markPointPolXList.value];
    } else if (channelName.includes('导高')) {
      return [...markPointYList.value, ...markPointPolYList.value];
    }
    return [];
  };

  // 重置所有标记点
  const resetMarkPoints = () => {
    markPointXList.value = [];
    markPointYList.value = [];
    markPointPolXList.value = [];
    markPointPolYList.value = [];
  };

  return {
    markPointXList,
    markPointYList,
    markPointPolXList,
    markPointPolYList,
    processRealtimeData,
    cleanupMarkPoints,
    mapDataToSeries,
    getMarkPointsForChannel,
    resetMarkPoints
  };
}